package lu.uni.adtool.ui;

import java.awt.BorderLayout;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


import lu.uni.adtool.adtree.ADTreeForGui;

import lu.uni.adtool.domains.Domain;

/**
 * A view of an dynamically added attiribute domain.
 * 
 * @author Piot Kordy
 * @version 
 */
public class DomainView<Type> extends JPanel implements ItemListener
{
  static final long serialVersionUID = 152365011570211223L;
  private DomainCanvas<Type> canvas;
  private int id;
  private JCheckBox labelBox;
  private JCheckBox allLabelBox;
  private JCheckBox nodeSizeBox;
  private JCheckBox markEditableBox;
  /**
   * Constructs a new instance.
   * 
   * @param tree associated Attack Defense tree.
   * @param newParent main window of the application
   * @param domain associated domain.
   * @param newId id of the view
   */
  public DomainView(final ADTreeForGui tree, final MainWindow newParent,
      final Domain<Type> domain,final int newId)
  {
    super(new BorderLayout());
    id = newId;
    canvas = new DomainCanvas<Type>(tree, domain,newParent);
    initLayout(domain);
  }
  /**
   * Initialize the layout of a panel.
   * @param domain associated domain
   * 
   */
  private void initLayout(Domain domain)
  {
    labelBox = new JCheckBox("Show Labels");
    labelBox.setMnemonic(KeyEvent.VK_L);
    labelBox.setSelected(false);
    labelBox.addItemListener(this);
    allLabelBox = new JCheckBox("Show Computed Values for Subtrees");
    allLabelBox.setMnemonic(KeyEvent.VK_V);
    allLabelBox.setSelected(true);
    allLabelBox.addItemListener(this);
    markEditableBox = new JCheckBox("Mark Editable Nodes");
    markEditableBox.setMnemonic(KeyEvent.VK_M);
    markEditableBox.setSelected(canvas.isMarkEditable());
    markEditableBox.addItemListener(this);
    canvas.setLocalExtentProvider(false);
    canvas.setShowLabels(false);
    canvas.setShowAllLabels(true);
    nodeSizeBox = new JCheckBox("Local Node Size");
    nodeSizeBox.setMnemonic(KeyEvent.VK_S);
    nodeSizeBox.setSelected(false);
    nodeSizeBox.addItemListener(this);
    final CollapsiblePanel optionsPanel = new CollapsiblePanel("Options");
    final JPanel checkBoxPanel = new JPanel();
    checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel,BoxLayout.LINE_AXIS));
    checkBoxPanel.add(labelBox);
    checkBoxPanel.add(nodeSizeBox);
    checkBoxPanel.add(markEditableBox);
    checkBoxPanel.add(allLabelBox);
    optionsPanel.add(checkBoxPanel);
    optionsPanel.toggleVisibility(true);
    final JScrollPane scrollPane = new JScrollPane(canvas);
    scrollPane.setAutoscrolls(true);
    canvas.setScrollPane(scrollPane);
    add(scrollPane);
    add(optionsPanel,BorderLayout.PAGE_END);
  }

  /** Listens to the check boxes. 
   * @param e event.
   */
  public final void itemStateChanged(final ItemEvent e)
  {
    final Object source = e.getItemSelectable();
    if (source == labelBox) {
      if (e.getStateChange() == ItemEvent.DESELECTED) {
        canvas.setShowLabels(false);
      }
      else {
        canvas.setShowLabels(true);
      }
    }
    if (source == allLabelBox) {
      if (e.getStateChange() == ItemEvent.DESELECTED) {
        canvas.setShowAllLabels(false);
      }
      else {
        canvas.setShowAllLabels(true);
      }
    }
    if (source == markEditableBox) {
      if (e.getStateChange() == ItemEvent.DESELECTED) {
        canvas.setMarkEditable(false);
      }
      else {
        canvas.setMarkEditable(true);
      }
    }
    if (source == nodeSizeBox) {
      if (e.getStateChange() == ItemEvent.DESELECTED) {
        canvas.setLocalExtentProvider(false);
      }
      else {
        canvas.setLocalExtentProvider(true);
      }
    }
  }

  /**
   * Retuns associated canvas.
   * 
   * @return canvas.
   */
  public final DomainCanvas<Type> getCanvas()
  {
    return canvas;
  }

  /**
   * Gets the id for this instance.
   *
   * @return The id.
   */
  public final int getId()
  {
    return this.id;
  }

  /**
   * Gets the mainWindow for this instance.
   *
   * @return The mainWindow.
   */
  public MainWindow getMainWindow()
  {
    return canvas.getMainWindow();
  }


  /**
   * Deregister canvas when windows is closed.
   * 
   */
  public final void onClose()
  {
    canvas.getTree().removeTreeChangeListener(canvas);
    canvas.getTree().deregisterSizeCanvas(canvas);
    if(canvas.getMainWindow().getLastFocused()==canvas){
      canvas.getMainWindow().setLastFocused(null);
    }
  }
  /**
   * Returns an icon for the view.
   * 
   * @return default icon
   */
  public final Icon getIcon()
  {
    return new ImageIcon(this.getClass().getResource("/icons/new.png"));
  }

}
